<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2020/8/17
 * Time: 13:56
 */

namespace app\admin\controller;

use think\Db;
use think\Request;

class District extends Base
{
    /**
     * 楼盘列表
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax ()){
            $params = input('param.');
            $dis_name = trim(input('dis_name'), ' ');
            $dis_kaifa = trim(input('dis_kaifa'), ' ');
            $where = [];
            if(isset($dis_name) && $dis_name != ''){
                $where['dis_name'] = ['like', '%' . $dis_name . '%'];
            }
            if(isset($dis_kaifa) && $dis_kaifa != ''){
                $where['dis_kaifa'] = ['like', '%' . $dis_kaifa . '%'];
            }
            if(isset($params['sheng']) && $params['sheng'] != ''){
                $where['dis_sheng'] = $params['sheng'];
            }
            if(isset($params['shi']) && $params['shi'] != ''){
                $where['dis_shi'] = $params['shi'];
            }
            if(isset($params['qu']) && $params['qu'] != ''){
                $where['dis_qu'] = $params['qu'];
            }
            $page = input('page') ? input('page'):1;
            $limit = input('limit')?input('limit'):10;// 获取总条数;
            $count = Db::name('district')->where($where)->count();
            $lists = Db::name('district')->alias('u')
                ->join('up_area a1', 'u.dis_sheng = a1.district_id', 'left')
                ->join('up_area a2', 'u.dis_shi = a2.district_id', 'left')
                ->join('up_area a3', 'u.dis_qu = a3.district_id', 'left')
                ->field('dis_id, dis_name, dis_kaifa, dis_wuyecom, dis_loudong, dis_mianji, dis_price, 
                dis_time,a1.district sheng,a2.district shi,a3.district qu')
                ->where($where)
                ->page($page,$limit)
                ->order('dis_time desc')
                ->select ();
            return json(['code'=>220,'msg'=>'','count'=>$count,'data'=>$lists]);
        }
        // 省
        $province = Db::table('up_area')->where('level', 1)->field('district_id, district')->select();
        $this->assign([
            'province' => $province
        ]);
        return $this->fetch();
    }
    
    /**
     * 添加小区
     * @return mixed|\think\response\Json
     */
    public function disAdd(){
        if(Request::instance()->isPost()){
            $dis_name = trim(input('dis_name'), ' ');
            $photo = input('photo');
            $dis_loudong = input('dis_loudong');
            $dis_mianji = input('dis_mianji');
            $dis_house = input('dis_house');
            $dis_part = input('dis_part');
            $dis_rongji = input('dis_rongji');
            $dis_wuyefei = input('dis_wuyefei');
            $dis_lvhua = input('dis_lvhua');
            $dis_kaifa = input('dis_kaifa');
            $dis_wuyecom = input('dis_wuyecom');
            $dis_price = input('dis_price');
            $dis_desc = input('dis_desc');
            $dis_louxing = input('dis_louxing');
            $dis_year = input('dis_year');
            $dis_shangq = input('dis_shangq');
            $dis_mendian = input('dis_mendian');
            $dis_sheng = input('dis_sheng');
            $dis_shi = input('dis_shi');
            $dis_qu = input('dis_qu');
            $dis_jiaot = input('dis_jiaot');
            $dis_address = input('dis_address');
            $dis_lon = input('dis_lon');
            $dis_lat = input('dis_lat');
            if(empty($photo)){
                return json(['code' => 100, 'msg' => '请上传图片']);
            }
            if(empty($dis_address) || empty($dis_lon) || empty($dis_lat)){
                return json(['code' => 100, 'msg' => '请填写详细地址']);
            }
            $insert = [
                'dis_name' => $dis_name,
                'dis_lat' => $dis_lat,
                'dis_lon' => $dis_lon,
                'dis_loudong' => $dis_loudong,
                'dis_mianji' => $dis_mianji,
                'dis_house' => $dis_house,
                'dis_part' => $dis_part,
                'dis_rongji' => $dis_rongji,
                'dis_wuyefei' => $dis_wuyefei,
                'dis_lvhua' => $dis_lvhua,
                'dis_kaifa' => $dis_kaifa,
                'dis_wuyecom' => $dis_wuyecom,
                'dis_price' => $dis_price,
                'dis_desc' => $dis_desc,
                'dis_images' => $photo,
                'dis_louxing' => $dis_louxing,
                'dis_year' => $dis_year,
                'dis_shangq' => $dis_shangq,
                'dis_mendian' => $dis_mendian,
                'dis_jiaot' => $dis_jiaot,
                'dis_address' => $dis_address,
                'dis_time' => date('Y-m-d H:i:s'),
                'dis_sheng' => $dis_sheng,
                'dis_shi' => $dis_shi,
                'dis_qu' => $dis_qu
            ];
            $res = Db::table('up_district')->insert($insert);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        // 省
        $province = Db::table('up_area')->where('level', 1)->field('district_id, district')->select();
        $this->assign([
            'province' => $province
        ]);
        return $this->fetch('district/disadd');
    }
    
    /**
     * 小区详情
     * @return mixed
     */
    public function disDetail(){
        $dis_id = input('dis_id');
        $dis = Db::table('up_district')
            ->where('dis_id', $dis_id)
            ->field('dis_name, dis_loudong, dis_mianji, dis_house, dis_part, dis_rongji, dis_wuyefei, 
            dis_lvhua, dis_kaifa, dis_wuyecom, dis_price, dis_desc, dis_images, dis_louxing, dis_year, 
            dis_shangq, dis_mendian, dis_jiaot, dis_address')
            ->find();
        // 图片
        $imgs = explode(',', trim($dis['dis_images'], ','));
        $dis['dis_images'] = $imgs;
        $this->assign([
            'dis' => $dis
        ]);
        return $this->fetch('district/disdetail');
    }
    
    /**
     * 编辑小区
     * @return mixed|\think\response\Json
     */
    public function disEdit(){
        $dis_id = input('dis_id');
        if(Request::instance()->isPost()){
            $dis_name = trim(input('dis_name'), ' ');
            $photo = input('photo');
            $dis_loudong = input('dis_loudong');
            $dis_mianji = input('dis_mianji');
            $dis_house = input('dis_house');
            $dis_part = input('dis_part');
            $dis_rongji = input('dis_rongji');
            $dis_wuyefei = input('dis_wuyefei');
            $dis_lvhua = input('dis_lvhua');
            $dis_kaifa = input('dis_kaifa');
            $dis_wuyecom = input('dis_wuyecom');
            $dis_price = input('dis_price');
            $dis_desc = input('dis_desc');
            $dis_louxing = input('dis_louxing');
            $dis_year = input('dis_year');
            $dis_shangq = input('dis_shangq');
            $dis_mendian = input('dis_mendian');
            $dis_jiaot = input('dis_jiaot');
            $dis_sheng = input('dis_sheng');
            $dis_shi = input('dis_shi');
            $dis_qu = input('dis_qu');
            $dis_address = input('dis_address');
            $dis_lon = input('dis_lon');
            $dis_lat = input('dis_lat');
            if(empty($photo)){
                return json(['code' => 100, 'msg' => '请上传图片']);
            }
            if(empty($dis_address) || empty($dis_lon) || empty($dis_lat)){
                return json(['code' => 100, 'msg' => '请填写详细地址']);
            }
            $update = [
                'dis_name' => $dis_name,
                'dis_lat' => $dis_lat,
                'dis_lon' => $dis_lon,
                'dis_loudong' => $dis_loudong,
                'dis_mianji' => $dis_mianji,
                'dis_house' => $dis_house,
                'dis_part' => $dis_part,
                'dis_rongji' => $dis_rongji,
                'dis_wuyefei' => $dis_wuyefei,
                'dis_lvhua' => $dis_lvhua,
                'dis_kaifa' => $dis_kaifa,
                'dis_wuyecom' => $dis_wuyecom,
                'dis_price' => $dis_price,
                'dis_desc' => $dis_desc,
                'dis_images' => $photo,
                'dis_louxing' => $dis_louxing,
                'dis_year' => $dis_year,
                'dis_shangq' => $dis_shangq,
                'dis_mendian' => $dis_mendian,
                'dis_jiaot' => $dis_jiaot,
                'dis_address' => $dis_address,
                'dis_update' => date('Y-m-d H:i:s'),
                'dis_sheng' => $dis_sheng,
                'dis_shi' => $dis_shi,
                'dis_qu' => $dis_qu
            ];
            $res = Db::table('up_district')->where('dis_id', $dis_id)->update($update);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $dis = Db::table('up_district')
            ->where('dis_id', $dis_id)
            ->field('dis_name, dis_loudong, dis_mianji, dis_house, dis_part, dis_rongji, dis_wuyefei, 
            dis_lvhua, dis_kaifa, dis_wuyecom, dis_price, dis_desc, dis_images, dis_louxing, dis_year, 
            dis_shangq, dis_mendian, dis_jiaot, dis_address, dis_lat, dis_lon, dis_sheng, dis_shi, dis_qu')
            ->find();
        // 省
        $province = Db::table('up_area')->where('level', 1)->field('district_id, district')->select();
        // 市
        $city = Db::table('up_area')
            ->where('level', 2)
            ->where('pid', $dis['dis_sheng'])
            ->field('district_id, district')
            ->select();
        // 区
        $district = Db::table('up_area')
            ->where('level', 3)
            ->where('pid', $dis['dis_shi'])
            ->field('district_id, district')
            ->select();
        $this->assign([
            'dis_id' => $dis_id,
            'dis' => $dis,
            'province' => $province,
            'city' => $city,
            'district' => $district
        ]);
        return $this->fetch('district/disedit');
    }
    
    /**
     * 删除小区
     * @return \think\response\Json
     */
    public function disDel(){
        $dis_id = input('id');
        $res = Db::table('up_district')->delete($dis_id);
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
    
    /**
     * 小区户型图
     * @return mixed|\think\response\Json
     */
    public function disImage(){
        if(request()->isAjax ()){
            $where['dh_dis_id'] = session('dis_id');
            $where['dh_type'] = 1;
            $page = input('page') ? input('page'):1;
            $limit = input("limit")?input("limit"):10;// 获取总条数;
            $count = Db::name('district_hu')->where($where)->count();
            $lists = Db::name('district_hu')
                ->field('dh_id, dh_name, dh_image, dh_time')
                ->where($where)
                ->page($page,$limit)
                ->order('dh_time desc')
                ->select ();
            return json(['code'=>220,'msg'=>'','count'=>$count,'data'=>$lists]);
        }
        $dis_id = input('dis_id');
        if($dis_id){
            session('dis_id', $dis_id);
        }
        return $this->fetch('district/disimage');
    }
    
    /**
     * 添加户型图
     * @return mixed|\think\response\Json
     */
    public function addImage(){
        if(Request::instance()->isPost()){
            $dh_name = trim(input('dh_name'), ' ');
            $dh_image = input('dh_image');
            $insert = [
                'dh_dis_id' => session('dis_id'),
                'dh_name' => $dh_name,
                'dh_image' => $dh_image,
                'dh_time' => date('Y-m-d H:i:s'),
                'dh_type' => 1
            ];
            $res = Db::table('up_district_hu')->insert($insert);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        return $this->fetch('district/addimage');
    }
    
    /**
     * 编辑户型图
     * @return mixed|\think\response\Json
     */
    public function editImage(){
        $dh_id = input('dh_id');
        if(Request::instance()->isPost()){
            $dh_name = trim(input('dh_name'), ' ');
            $dh_image = input('dh_image');
            $update = [
                'dh_name' => $dh_name,
                'dh_image' => $dh_image,
                'dh_updatetime' => date('Y-m-d H:i:s'),
            ];
            $res = Db::table('up_district_hu')->where('dh_id', $dh_id)->update($update);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $img = Db::table('up_district_hu')
            ->field('dh_name, dh_image')
            ->where('dh_id', $dh_id)
            ->find();
        $this->assign([
            'dh_id' => $dh_id,
            'img' => $img
        ]);
        return $this->fetch('district/editimage');
    }
    
    /**
     * 删除户型图
     * @return \think\response\Json
     */
    public function delImage(){
        $dh_id = input('id');
        $res = Db::table('up_district_hu')->delete($dh_id);
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
}